package com.glorystone.ins.service.impl;

import com.glorystone.ins.common.common.PageVo;
import com.glorystone.ins.dao.InsurerMapper;
import com.glorystone.ins.domain.Insurer;
import com.glorystone.ins.domain.InsurerUser;
import com.glorystone.ins.interceptor.PageList;
import com.glorystone.ins.service.InsurerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author:LiuSC
 * @Date:Created in 2018-03-13 15:48
 * @since: JDK 1.7
 * @Description:
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class InsurerServiceImpl implements InsurerService {

    @Autowired
    private InsurerMapper insurerMapper;

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean addInsuer(Insurer insurer) {
        int i = insurerMapper.insert(insurer);
        if(i != 0){
            Long userId = insurer.getUserId();
            if(userId !=null){
                InsurerUser iUser = new InsurerUser();
                iUser.setInsurerId(insurer.getTid());
                iUser.setUserId(insurer.getUserId());
                iUser.setUserName(insurer.getUserName());
                int j = insurerMapper.insertInsurerUser(iUser);
                return j==0 ? false : true;
            }
            return true;
        }
        return false;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean updateInsuer(Insurer insurer) {
        int i = insurerMapper.update(insurer);
        int count = insurerMapper.getInsurerUserCount(insurer.getTid());
        if(i != 0){
            if(count<1){
                Long userId = insurer.getUserId();
                if(userId !=null){
                    InsurerUser iUser = new InsurerUser();
                    iUser.setInsurerId(insurer.getTid());
                    iUser.setUserId(insurer.getUserId());
                    iUser.setUserName(insurer.getUserName());
                    int j = insurerMapper.insertInsurerUser(iUser);
                    return j==0 ? false : true;
                }
            }else{
                InsurerUser iUser = new InsurerUser();
                iUser.setInsurerId(insurer.getTid());
                iUser.setUserId(insurer.getUserId());
                iUser.setUserName(insurer.getUserName());
                int j = insurerMapper.updateInsurerUser(iUser);
                return j==0 ? false : true;
            }
            return true;
        }
        return false;
    }

    @Override
    public Insurer getInsurerById(Long tid) {
        return insurerMapper.selectByPrimaryKey(tid);
    }

    @Override
    public PageList<Insurer> queryAllInsurerList(PageVo vo) {
        return insurerMapper.queryAllInsurerList(vo,vo.getPageable());
    }

    @Override
    public List<Insurer> selectAllInsurer() {
        return insurerMapper.selectAllInsurer();
    }

    @Override
    public List<InsurerUser> getSlaesmenByCompanyId(Long tid) {
        return insurerMapper.getSlaesmenByCompanyId(tid);
    }

    /**
     * 查询业务员对应的保险公司
     * @param userId
     * @return
     */
    @Override
    public List<InsurerUser> getSlaesmenByUserId(Long userId) {
        return insurerMapper.getSlaesmenByUserId(userId);
    }
}
